package banco;

import java.util.List;

import javax.persistence.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;
import modelo.Cliente;

public class BancoDeClientes {

	public void salvar(Cliente u) {
		Session sessao = null;
		Transaction tx = null;
		try {
			sessao = HibernateUtil.getSession();
			tx = sessao.beginTransaction();
			sessao.save(u);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			ex.printStackTrace();
		} finally {
			if (sessao != null) {
				try {
					sessao.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public List<Cliente> recuperar() {
		Session sessao = null;
		Transaction tx = null;
		List<Cliente> clientes = null;
		try {
			sessao = HibernateUtil.getSession();
			tx = sessao.beginTransaction();
			String nomeCliente = "Vitor";
			org.hibernate.Query q = sessao.createQuery("from Cliente where nome ='" + nomeCliente+"'");
			clientes = q.list();
		} catch (Exception ex) {
			tx.rollback();
			ex.printStackTrace();
		} finally {
			if (sessao != null) {
				try {
					sessao.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return clientes;
	}

}
